from rest_framework.generics import ListAPIView, ListCreateAPIView
from apps.meiduo_admin.serializers.user import UserSerializer, UserCreateSerializer
from apps.meiduo_admin.utils import PageNum
from apps.users.models import User


class UserListCreateAPIView(ListCreateAPIView):

    pagination_class = PageNum

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')
        if keyword is None or keyword == '':
            queryset = User.objects.all()
        else:
            queryset = User.objects.filter(username__contains=keyword)
        return queryset

    def get_serializer_class(self):
        if self.request.method == 'GET':
            return UserSerializer
        else:
            return UserCreateSerializer